package com.ugoodtech.mdcc.core.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Kotone
 * Date: 2017/9/12
 * Time: 19:10
 * To change this template use File | Settings | File Templates.
 */
public class ModelUtil {

    public static String getModelDescription(File domainFile) throws IOException {
        if (null != domainFile) {
            BufferedReader reader = new BufferedReader(new FileReader(domainFile));
            String line = reader.readLine();
            String description = "";
            while (line != null) {
                int indexTable = line.indexOf("@ApiModel(");
                if (indexTable != -1) {
                    String desc = line.split("\"")[1];
                    return desc;
                }
                line = reader.readLine();
            }
        }
        return domainFile.getName();
    }

    public static List<ModelPro> getKnownProperties(File domainFile) throws IOException {

        List<ModelPro> domainProperties = new ArrayList<>();
        if (null != domainFile) {
            BufferedReader reader = new BufferedReader(new FileReader(domainFile));
            String line = reader.readLine();
            int index = 0;
            List<String> codes = new ArrayList<String>();
            boolean start = false;
            boolean isProperty = false;
            String code = "";
            while (line != null) {
                int indexTable = line.indexOf("@ApiModelProperty");
                if (-1 != indexTable) {
                    start = true;
                    code += line;
                } else {
                    if (start) {
                        code += line;
                        codes.add(code);
                        code = "";
                        start = false;
                    }
                }
                line = reader.readLine();
            }
            for (String cd : codes) {

                String str[] = cd.split("\"");
                String desc = str[1];
                List<String> fields = getField(str[2]);

                try {
                    ModelPro modelPro = new ModelPro(fields.get(2), fields.get(3), desc);
                    domainProperties.add(modelPro);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return domainProperties;
        }
        return null;

    }

    public static List<ModelPro> getUniqueProperties(File domainFile) throws IOException {
        String[] keys = {"code", "key"};
        String[] strKeys = {"str"};
        List<ModelPro> modelPros = getKnownProperties(domainFile);
        List<ModelPro> uniquePros = new ArrayList<>();
        for (ModelPro modelPro : modelPros) {
            String pro = modelPro.getField().toLowerCase();
            for (String key : keys) {
                if (-1 != pro.indexOf(key)) {
                    for (String key2 : strKeys) {
                        if (!pro.contains(key2)) {
                            uniquePros.add(modelPro);
                        }
                    }
                }
            }
        }
        return uniquePros;
    }


    public static List<ModelPro> getUniqueStringProperties(File domainFile) throws IOException {
        String[] keys = {"code", "key"};
        String[] strKeys = {"str"};
        List<ModelPro> modelPros = getKnownProperties(domainFile);
        List<ModelPro> uniquePros = new ArrayList<>();
        for (ModelPro modelPro : modelPros) {
            String pro = modelPro.getField().toLowerCase();
            for (String key : keys) {
                if (-1 != pro.indexOf(key)) {
                    for (String key2 : strKeys) {
                        if (!pro.contains(key2)) {
                            if ("String".equals(modelPro.getType())) {
                                uniquePros.add(modelPro);
                            }
                        }
                    }
                }
            }
        }
        return uniquePros;
    }


    public static List<ModelPro> getModelProperties(File domainFile) throws IOException {
        String[] keys = {"code", "key"};
        String[] strKeys = {"str"};
        List<ModelPro> modelPros = getKnownProperties(domainFile);
        List<ModelPro> uniquePros = new ArrayList<>();
        for (ModelPro modelPro : modelPros) {
            String pro = modelPro.getField().toLowerCase();
            for (String key : keys) {
                if (-1 != pro.indexOf(key)) {
                    for (String key2 : strKeys) {
                        if (!pro.contains(key2)) {
                            if ("String".equals(modelPro.getType())) {
                                uniquePros.add(modelPro);
                            }
                        }
                    }
                }
            }
        }
        return uniquePros;
    }



    //    private static List<String> getField(String code){
//        String[] str=code.split(" ");
//        List<String> key=new ArrayList<>();
//        for(String st:str){
//            if(!StringUtils.isEmpty(st.trim())){
//                key.add(st.trim().split(";")[0]);
//            }
//        }
//        return key;
//    }
    private static List<String> getField(String code) {
        String[] str1 = code.split("\\)");
        String[] str = null;
        if (str1.length > 1) {
            str = str1[1].split(" ");
        } else {
            str = str1[0].split(" ");
        }

        List<String> key = new ArrayList<>();
        for (String st : str) {
            if (!StringUtils.isEmpty(st.trim())) {
                String[] keys = st.trim().split(";");
                if (keys.length > 0) {
                    key.add(st.trim().split(";")[0]);
                } else {
                    System.out.println(keys);
                }

            }
        }
        return key;
    }

    public static String getGetMethod(String field) {
       return  "get"+upperCase(field);
    }

    public static String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }
}
